package com.study.sort;

public class Tree {

	int data; //节点的值
	public static Tree root;//根节点(有且仅有一个)
	public Tree leftSon;
	public Tree rightSon;
	
	public Tree(int data){
		this.data = data;
		this.leftSon = null;
		this.rightSon = null;
	}
	public Tree(){
		
	}
	
	public void insert(int data){
		if(root ==null){
			root = new Tree(data);
		}else {
			insert(root,data);
			
		}
		
		//return root;
	}
	
	public void insert(Tree root, int data){
		//Tree tmp = new Tree(data);
		
		if(root.data > data){
			if(root.leftSon == null){
				root.leftSon = new Tree(data);
			}else {
				insert(root.leftSon,data);
			}
		}else {
			if(root.rightSon == null ){
				root.rightSon = new Tree(data);
			}else {
				insert(root.rightSon,data);
			}
			
		}
	}
	
	public static void main(String[] args) {
		
		Integer[] array = { 1, 6, 2, 3, 8, 5, 7, 4 };
		
		Tree root1 = new Tree();
		for(int i = 0; i < array.length; i++){
			root1.insert(array[i]);	
		}
		
		outPutTree(Tree.root);
	}
	
	//遍历树中的集合
    public static void outPutTree(Tree tree){
        if(tree !=null){
        	if(tree.leftSon != null){
                outPutTree(tree.leftSon);
            }
        	System.out.print(tree.data+" ");
        	if(tree.rightSon != null){
                outPutTree(tree.rightSon);
            }
        }
    }
}
